﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LevelEditor.model.core;

namespace LevelEditor.model.elements
{
    public abstract class AbstractPGameObject
    {
        #region Variables
        protected int m_id;
        protected int m_resId;
        protected string m_name;
        protected string m_type;
        protected float m_x;
        protected float m_y;
        protected float m_scaleX;
        protected float m_scaleY;
        protected float m_orientationAngle;
        protected Boolean m_isActive;
        protected Boolean m_isOn;
        protected Picture m_picture;
        protected List<AbstractPGameObject> m_linkedPGOList;

        #endregion

        #region Constructeurs
        public AbstractPGameObject(string name, Dictionary<String, String> pGameObjectData)
        {
            m_id = -1;
            m_name = name;
            m_x = 0;
            m_y = 0;
            m_scaleX = 1;
            m_scaleY = 1;
            m_orientationAngle = 0;
            m_isActive = false;
            m_isOn = false;
            m_linkedPGOList = null;

            initDictionaryParams(pGameObjectData);
        }

        #endregion

        #region Methodes
		public void initDictionaryParams(Dictionary<String, String> pGameObjectData)
		{
			if (pGameObjectData.ContainsKey("id"))
			{
				m_id = int.Parse(pGameObjectData["id"]);
			}

			if (pGameObjectData.ContainsKey("editName"))
			{
				m_name = pGameObjectData["editName"];
			}

			if (pGameObjectData.ContainsKey("type"))
			{
				m_type = pGameObjectData["type"];
			}

			if (pGameObjectData.ContainsKey("positionX"))
			{
				m_x = float.Parse(pGameObjectData["positionX"]);
			}

			if (pGameObjectData.ContainsKey("positionY"))
			{
				m_y = float.Parse(pGameObjectData["positionY"]);
			}

			if (pGameObjectData.ContainsKey("scaleX"))
			{
				pGameObjectData["scaleX"] = pGameObjectData["scaleX"].Replace(".", ",");
				m_scaleX = float.Parse(pGameObjectData["scaleX"]);
			}

			if (pGameObjectData.ContainsKey("scaleY"))
			{
				pGameObjectData["scaleY"] = pGameObjectData["scaleY"].Replace(".", ",");
				m_scaleY = float.Parse(pGameObjectData["scaleY"]);
			}

            if (pGameObjectData.ContainsKey("orientation"))
            {
                m_orientationAngle = float.Parse((pGameObjectData["orientation"]));
            }

			if (pGameObjectData.ContainsKey("activated"))
			{
				m_isActive = true;
			}
			else
			{
				m_isActive = false;
			}

			if (pGameObjectData.ContainsKey("url"))
			{
				m_picture = new Picture(pGameObjectData["url"], float.Parse(pGameObjectData["sizeX"]), float.Parse(pGameObjectData["sizeY"]));
			}
            else if (pGameObjectData.ContainsKey("resId"))
			{
                m_resId = int.Parse(pGameObjectData["resId"]);
                String xmlFileName = Library.usedAssetsList[m_resId];
                String url = Library.getUrlLinkedToXml(xmlFileName);

                m_scaleX = float.Parse(pGameObjectData["scaleX"].Replace('.', ','));
                m_scaleY = float.Parse(pGameObjectData["scaleY"].Replace('.', ','));

                float sizeX = Library.getSizeXWithXmlFileName(xmlFileName);
                float sizeY = Library.getSizeYWithXmlFileName(xmlFileName);

                m_picture = new Picture(url, sizeX, sizeY);
            }
            else
            {
                m_picture = null;
                Console.WriteLine("WTF with the picture at loading");
            }
		}

        public virtual Dictionary<string, string> getViewData()
        {
            Dictionary<string, string> data = new Dictionary<string, string>();

            data["name"] = m_name;
            data["x"] = m_x.ToString();
            data["y"] = m_y.ToString();
            data["scaleX"] = m_scaleX.ToString();
            data["scaleY"] = m_scaleY.ToString();
            data["orientationAngle"] = m_orientationAngle.ToString();
            data["type"] = m_type;

            return data;
        }

        public void addLinkedPGO(AbstractPGameObject pGameObject)
        {
            if (m_linkedPGOList == null)
            {
                m_linkedPGOList = new List<AbstractPGameObject>();
            }

            if (m_linkedPGOList.Contains(pGameObject))
            {
                return;
            }

            m_linkedPGOList.Add(pGameObject);
        }

        #endregion

        #region Getters && Setters
        public int id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public int resId
        {
            get { return m_resId; }
            set { m_resId = value; }
        }

        public string name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public string type
        {
            get { return m_type; }
            set { m_type = value;}
        }

        public float x
        {
            get { return m_x; }
            set { m_x = value; }
        }

        public float y
        {
            get { return m_y; }
            set { m_y = value; }
        }

        public float scaleX
        {
            get { return m_scaleX; }
            set { m_scaleX = value; }
        }

        public float scaleY
        {
            get { return m_scaleY; }
            set { m_scaleY = value; }
        }

        public float orientationAngle
        {
            get { return m_orientationAngle; }
            set { m_orientationAngle = value; }
        }

        public Boolean isActive
        {
            get { return m_isActive; }
            set { m_isActive = value; }
        }

        public Boolean isOn
        {
            get { return m_isOn; }
        }

        public Picture picture
        {
            get { return m_picture; }
            set { m_picture = value; }
        }

        public Bitmap getBmp
        {
            get { return m_picture.picture; }
        }

        public List<AbstractPGameObject> linkedPGOList
        {
            get { return m_linkedPGOList; }
        }

        #endregion
    }
}
